package com.sholmes.android.scribbler;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.widget.TextView;
import android.widget.ImageView;

public class ImageThread implements Runnable
{
	int[] originalImageData;
	int[] outputImageData;
	Bitmap bitmap, redBitmap;
	TextView statusText;
	ImageView imageView, redImageView;
	
	public ImageThread(int[] rawImageData, Bitmap bm, ImageView img, TextView status, Bitmap redBM, ImageView redImg)
	{
		originalImageData = rawImageData;
		bitmap = bm;
		statusText = status;
		imageView = img;
		redImageView = redImg;
		redBitmap = redBM;
		outputImageData = new int[256 * 192 * 3];
		
		//Thread t = new Thread(this);
		//t.start();
	}
	
	public void run()
	{
		statusText.setText("Loading Image...");
		YUVToRGB();
		
		int color, currentLocation;
		for(int i = 0; i < 192; i++)
		{
			for(int j = 0; j < 256; j++)
			{					
				currentLocation = (i * 256 + j) * 3;
				
				// set bitmap image data to our image data
				color = Color.argb(	255, 									// A
									outputImageData[currentLocation], 		// R
									outputImageData[currentLocation + 1], 	// G
									outputImageData[currentLocation + 2]); 	// B
				bitmap.setPixel(j, i, color);
			}
		}
		
		imageView.setImageBitmap(bitmap);
		
		statusText.setText("Processing Image...");
		SetRedImage(outputImageData);
		
		statusText.setText("Finished");
	}
	
	private void SetRedImage(int[] data)
	 {
		 int color, current, r, g, b;
		 
		for(int i = 0; i < 192; i++)
		{
			for(int j = 0; j < 256; j++)
			{
				current = (i * 256 + j) * 3;
				
				r = data[current];
				g = data[current + 1];
				b = data[current + 2];
				
				//if(r > g && r > b)
				if(getRedness(r, g, b) > 1.5f)
					r = 255;
				else
					r = 0;
				
				g = 0;
				b = 0;

				
				color = Color.argb(255, r, g, b);
				redBitmap.setPixel(j, i, color);
			}
		}
		
		redImageView.setImageBitmap(redBitmap);
	 }
	 
	 private float getRedness(int r, int g, int b)
	 {
		 float gbAvg = ((float)g + (float)b) / 2.0f;
		 float d = (float)r / gbAvg;
		 
		 return d;
	 }
	
	private void YUVToRGB()
	{
		int y = 0, u = 0, v = 0, R, G, B, current;
		int vy, vu, y1v, y1u, uy, uv, y2u, y2v;
		int mask = 0xFF;
		 
		for(int i = 0; i < 192; i++)
		{
			for(int j = 0; j < 256; j++)
			{
				if(j >= 3)
				{
					vy = -1; vu = -2; y1v = -1; y1u = -3; uy = -1; uv = -2; y2u = -1; y2v = -3;
				}
				else
				{
					vy = 1; vu = 2; y1v = 3; y1u = 1; uy = 1; uv = 2; y2u = 3; y2v = 1;
				}
				
				if((j % 4) == 0)
				{
					v = originalImageData[i * 256 + j]      & mask;
					y = originalImageData[i * 256 + j + vy] & mask;
					u = originalImageData[i * 256 + j + vu] & mask;
				}
				else if((j % 4) == 1)
				{
					y = originalImageData[i * 256 + j]       & mask;
					v = originalImageData[i * 256 + j + y1v] & mask;
					u = originalImageData[i * 256 + j + y1u] & mask;
				}
				else if((j % 4) == 2)
				{
					u = originalImageData[i * 256 + j]      & mask;
					y = originalImageData[i * 256 + j + uy] & mask;
					v = originalImageData[i * 256 + j + uv] & mask;
				}
				else if((j % 4) == 3)
				{
					y = originalImageData[i * 256 + j]       & mask;
					u = originalImageData[i * 256 + j + y2u] & mask;
					v = originalImageData[i * 256 + j + y2v] & mask;
				}
				
				// 0x80 = 128
				u = u - 0x80; // masking these makes the colors go all pink
				v = v - 0x80;
				
				R = Math.max( Math.min( (int)(y + 1.13983 * v),               255 ), 0 ) & mask;
				G = Math.max( Math.min( (int)(y - 0.39466 * u - 0.58060 * v), 255 ), 0 ) & mask;
				B = Math.max( Math.min( (int)(y + 2.03211 * u),               255 ), 0 ) & mask;
				
				current = (i * 256 + j) * 3;
				
				outputImageData[current]     = R;
				outputImageData[current + 1] = G;
				outputImageData[current + 2] = B;
			}
		}
	}
}






